package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;

import com.inossem.utils.base.ArithUtils;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameOverviewBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/7/5.
 */

public class UpFrameUtils {

    public static UpFrameOverviewBean getUpFrameOverView(Context context) {
        return SPUtils.getUpFrameInformation(context);
    }

    public static void saveUpFrameOverview(Context context, UpFrameOverviewBean bean) {
        SPUtils.saveUpFrameInformation(context, bean);
    }

    //过滤上架货品列表信息
    public static List<UpFrameOverviewBean.DataBean.MaterielListBean> getUpFrameMaterielList(Context context, String materiel) {
        List<UpFrameOverviewBean.DataBean.MaterielListBean> upFrameList = new ArrayList<>();
        List<UpFrameOverviewBean.DataBean.MaterielListBean> allUpFrameList = getUpFrameOverView(context).getData().getMaterielList();
        for (int i = 0; i < allUpFrameList.size(); i++) {
            UpFrameOverviewBean.DataBean.MaterielListBean item = allUpFrameList.get(i);
            if (item.getMaterielCode().contains(materiel) || item.getMaterielDescription().contains(materiel)) {
                upFrameList.add(item);
                continue;
            }
        }
        return upFrameList;
    }

    //通过序列号获取物料信息
    public static UpFrameOverviewBean.DataBean.MaterielListBean getMaterielBySerial(Context context, String serial) {
        List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList = getUpFrameOverView(context).getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            if (materielList.get(i).getSerial().equals(serial)) {
                return materielList.get(i);
            }
        }
        return null;
    }

    //通过物料序列号更新仓位列表
    public static void updatePositionListBySerial(Context context, String serial, UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem) {
        UpFrameOverviewBean bean = getUpFrameOverView(context);
        List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList = getUpFrameOverView(context).getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            UpFrameOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getSerial().equals(serial)) {
                List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList = materielItem.getPositionList() == null ? new ArrayList() : materielItem.getPositionList();
                positionList.add(positionItem);
                materielItem.setPositionList(positionList);
                materielList.set(i, materielItem);
                break;
            }
        }
        bean.getData().setMaterielList(materielList);
        saveUpFrameOverview(context, bean);
    }

    //通过序列号和id删除仓位
    public static void deletePositionNumberByIdAndSerial(Context context, String id, String serial) {
        UpFrameOverviewBean bean = getUpFrameOverView(context);
        List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList = getUpFrameOverView(context).getData().getMaterielList();
        out:
        for (int i = 0; i < materielList.size(); i++) {
            UpFrameOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getSerial().equals(serial)) {
                List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList = materielItem.getPositionList() == null ? new ArrayList() : materielItem.getPositionList();
                in:
                for (int j = 0; j < positionList.size(); j++) {
                    UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem = positionList.get(j);
                    if (positionItem.getId().equals(id)) {
                        positionList.remove(positionItem);
                        materielItem.setPositionList(positionList);
                        materielList.set(i, materielItem);
                        break out;
                    }
                }
            }
        }
        bean.getData().setMaterielList(materielList);
        saveUpFrameOverview(context, bean);
    }

    //获取上架提交数量
    public static int getUpFrameConfirmNumber(Context context) {
        int result = 0;
        List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList = getUpFrameOverView(context).getData().getMaterielList();
        if (materielList == null) {
            return result;
        } else {
            for (int i = 0; i < materielList.size(); i++) {
                List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList = materielList.get(i).getPositionList();
                if (positionList != null && positionList.size() > 0) {
                    result = result + 1;
                }
            }
        }
        return result;
    }


    public static UpFrameConfirmRequestBean getUpFrameConfirmRequestBean(Context context) {
        UpFrameConfirmRequestBean result = new UpFrameConfirmRequestBean();
        UpFrameOverviewBean bean = getUpFrameOverView(context);
        result.setRequestNumber(bean.getData().getRequestNumber());
        List<UpFrameConfirmRequestBean.MaterielListBean> resultMaterielList = new ArrayList<>();
        List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList = bean.getData().getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            UpFrameConfirmRequestBean.MaterielListBean resultMaterielItem = new UpFrameConfirmRequestBean.MaterielListBean();
            UpFrameOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            resultMaterielItem.setSerial(materielItem.getSerial());

            List<UpFrameConfirmRequestBean.MaterielListBean.PositionListBean> resultPositionList = new ArrayList<>();
            List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList = materielItem.getPositionList();
            if (positionList != null && !positionList.isEmpty()) {
                for (int j = 0; j < positionList.size(); j++) {
                    UpFrameConfirmRequestBean.MaterielListBean.PositionListBean resultPositionItem = new UpFrameConfirmRequestBean.MaterielListBean.PositionListBean();
                    UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem = positionList.get(j);
                    resultPositionItem.setPosition(positionItem.getPosition());
                    resultPositionItem.setNumber(positionItem.getNumber());
                    resultPositionList.add(resultPositionItem);
                }
            }
            resultMaterielItem.setPositionList(resultPositionList);
            if (!resultMaterielItem.getPositionList().isEmpty()) {
                resultMaterielList.add(resultMaterielItem);
            }
        }
        result.setMaterielList(resultMaterielList);
        return result;
    }

    public static boolean checkMaterielItem(UpFrameOverviewBean.DataBean.MaterielListBean materielItem) {
        List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList = materielItem.getPositionList();
        if (positionList != null && !positionList.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static BigDecimal getNumber(List<UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean> positionList) {
        BigDecimal result = BigDecimal.ZERO;
        if (positionList != null && !positionList.isEmpty()) {
            for (int i = 0; i < positionList.size(); i++) {
                UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem = positionList.get(i);
                result = ArithUtils.add(result.toString(), positionItem.getNumber());
            }
            return result;
        }
        return result;
    }

    //获取转储行项目概览
    public static List<OverviewAdapter.OverviewBean> getOverViewBeanWithUpFrameList(List<UpFrameOverviewBean.DataBean.MaterielListBean> materielList) {
        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
        for (int i = 0; i < materielList.size(); i++) {
            UpFrameOverviewBean.DataBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getPositionList() != null && !materielItem.getPositionList().isEmpty()) {
                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
                overItem.setInfo(materielItem.getMaterielDescription());
                overItem.setNumber(getNumber(materielItem.getPositionList()).toPlainString() + materielItem.getUnit());
                resultList.add(overItem);
            }
        }
        return resultList;
    }
}
